package com.yy.controller.service.Impl;

import com.yy.context.BaseContext;
import com.yy.controller.mapper.AddressBookMapper;
import com.yy.controller.service.AddressBookService;
import com.yy.entity.AddressBook;
import com.yy.vo.AddressBookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;

    /**
     * 获取地址列表
     *
     * @return
     */
    @Override
    public List<AddressBookVo> list() {
        Long userId = BaseContext.getCurrentId();

        List<AddressBook> addressBookList = addressBookMapper.list(userId);
        List<AddressBookVo> addressBookVoList = new ArrayList<>();
        //封装List<AddressBookVo>
        for (AddressBook addressBook : addressBookList) {
            AddressBookVo addressBookVo = new AddressBookVo();
            addressBookVo.setId(addressBook.getId());
            //判断联系人性别
            if (addressBook.getSex() == 0) {
                addressBookVo.setName(addressBook.getConsignee() + " " + "女士");
            }
            if (addressBook.getSex() == 1) {
                addressBookVo.setName(addressBook.getConsignee() + " " + "先生");
            }
            addressBookVo.setTel(addressBook.getPhone());
            addressBookVo.setAddress(addressBook.getBuilding() + " " + addressBook.getDoorplate());
            addressBookVo.setIsDefault(addressBook.getIsDefault());
            addressBookVoList.add(addressBookVo);
        }
        return addressBookVoList;
    }

    /**
     * 添加地址
     *
     * @param addressBook
     */
    @Override
    public void add(AddressBook addressBook) {
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        //若设置为默认先查看地址表是否有默认地址
        if (addressBook.getIsDefault()==1){
            //获取该用户的地址列表
            List<AddressBook> list = addressBookMapper.list(userId);
            for (AddressBook address : list) {
                if (address.getIsDefault() == 1) {
                    address.setIsDefault(0);
                    addressBookMapper.update(address);
                }
            }
        }
        //添加到地址表
        addressBookMapper.add(addressBook);
    }

    /**
     * 修改地址
     * @param addressBook
     */
    @Override
    public void upadte(AddressBook addressBook) {
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        //若设置为默认先查看地址表是否有默认地址
        if (addressBook.getIsDefault()==1){
            //获取该用户的地址列表
            List<AddressBook> list = addressBookMapper.list(userId);
            for (AddressBook address : list) {
                if (address.getIsDefault() == 1) {
                    address.setIsDefault(0);
                    addressBookMapper.update(address);
                }
            }
        }
        //修改地址
        addressBookMapper.update(addressBook);
    }

    /**
     * 根据id删除地址
     * @param id
     */
    @Override
    public void delete(Long id) {
        addressBookMapper.delete(id);
    }
}
